package operator.function;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.BooleanSupplier;
import util.Printer;
import util.RxJavaHelper;
import util.ThreadHelper;

class RetryUntilOperator {

    static int tryTimes;

    public static void main(String[] args) {
        retryUntil();
    }

    /**
     * 错误触发
     * 知道为true时不再尝试
     */
    static void retryUntil() {
        Observable.create(new ObservableOnSubscribe<Integer>() {

            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                tryTimes++;
                Printer.print("已经尝试了" + tryTimes + "次");
                emitter.onError(RxJavaHelper.getException());
            }
        })
                .retryUntil(new BooleanSupplier() {
                    @Override
                    public boolean getAsBoolean() throws Exception {
                        if (tryTimes < 5) {
                            return false;
                        } else {
                            return true;
                        }
                    }
                })
                .subscribe(RxJavaHelper.getConsumer());
        ThreadHelper.sleep();

    }


}
